<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Controllers\Controller;

use App\Repositories\TaskRepository;

class TaskController extends Controller
{
    /**
     * 任务资源库的实例。
     *
     * @var TaskRepository
     */
    protected $tasks;

    /**
     * 创建一个新的控制器实例
     * @param  TaskRepository  $tasks
     * @return void
     */
    public function __construct(TaskRepository $tasks)
    {
        $this->middleware('auth');
        $this->tasks = $tasks;
    }

    /**
     * 显示用户所有任务的清单
     * @param  Request  $request
     * @return Response
     */
    public function index(Request $request)
    {
//        $tasks =  Task::where('user_id', $request->user()->id())->get();
        return view('tasks.index', [
            'tasks' => $this->tasks->forUser($request->user())
        ]);
    }

    /**
     * 创建新任务
     * @param Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|max:255'
        ]);

        // 创建任务
        $request->user()->tasks()->create([
            'name' => $request->name
        ]);

        return redirect('/tasks');

    }

    /**
     * 删除指定任务
     * @param Request $request
     * @param Task $task
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function destroy(Request $request, Task $task)
    {
        //
        $this->authorize('destroy', $task);

        $task->delete();

        return redirect('/tasks');
    }


}
